package com.depth.viewer.container
{
	import com.depth.viewer.events.AppEvent;
	import com.depth.viewer.facade.IocAppFacade;
	import com.depth.viewer.loader.IWidgetLoader;
	import com.depth.viewer.loader.WidgetLoader;
	import com.depth.viewer.manager.IWidgetDisplayManager;
	import com.depth.viewer.manager.IWidgetManager;
	import com.depth.viewer.manager.ViewerSystemManager;
	import com.depth.viewer.widget.IBaseWidget;
	import com.depth.viewer.widget.IWidget;
	import com.depth.viewer.widget.definition.IBaseWidgetDefinition;
	
	import mx.events.FlexEvent;
	
	public class WidgetLoaderContainer extends BaseWidgetContainer implements IWidgetLoaderContainer
	{
		protected var _loader:IWidgetLoader;
		protected var _widgetManager:IWidgetManager;
		
		protected var _runingParams:Object;
		
		protected var _autoLoad:Boolean = true;
		protected var _loaded:Boolean = false;
		
		public function WidgetLoaderContainer()
		{
			super();
			_widgetManager = ViewerSystemManager.getInstance().widgetManager;
			_loader = new WidgetLoader();
			
			ViewerContainer.addEventListener(IocAppFacade.WIDGET_LOADED,widgetLoadedHandler);
			/*ViewerContainer.addEventListener(IocAppFacade.WIDGET_REMOVED,widgetRemovedHandler);
			ViewerContainer.addEventListener(IocAppFacade.WIDGET_UNLOADED,widgetunLoadedHandler);*/
			
			addEventListener(FlexEvent.CREATION_COMPLETE,creationCompleteHandlder);
		}
		
		private function creationCompleteHandlder(event:FlexEvent):void{
			if(_autoLoad && !_loaded){
				load();
			}
		}
		
		protected function widgetLoadedHandler(event:AppEvent):void{
			var widget:IBaseWidget = event.data as IBaseWidget;
			if(widget && widget.widgetId == widgetId){
				var def:IBaseWidgetDefinition = widget.definition as IBaseWidgetDefinition;
				def.popupMode = false;
				def.modalPopup = false;
				def.centerPopupMode = false;
				def.left = undefined;
				def.right = undefined;
				def.bottom = undefined;
				def.top = undefined;
				def.x = 0;
				def.y = 0;
				
				IWidgetDisplayManager(_widgetManager).addWidgetDisplay(widget,this,false,false,false,true);
				
				_loaded = true;
			}
		}
		protected function widgetRemovedHandler(event:AppEvent):void{
			var widget:IBaseWidget = event.data as IBaseWidget;
			
			if(widget && widget.widgetId == widgetId){
				IWidgetDisplayManager(_widgetManager).removeWidgetDsiplay(widget);
			}
		}
		protected function widgetunLoadedHandler(event:AppEvent):void{
			var widget:IBaseWidget = event.data as IBaseWidget;
			
			if(widget && widget.widgetId == widgetId){
				IWidgetDisplayManager(_widgetManager).removeWidgetDsiplay(widget);
			}
		}
		
		
		public function set autoLoad(value:Boolean):void{
			this._autoLoad = value;
		}
		
		
		public function load():void
		{
			try{
				_widgetManager.loadWidget(widgetId,false,widgetTitle,_runingParams);
			}catch(err:Error){
				trace(err.getStackTrace());
			}
		}
		
		public function remove(id:String = null):void{
			try{
				_widgetManager.removeWidget(id?id:widgetId);
			}catch(err:Error){
				trace(err.getStackTrace());
			}
		}
		
		public function unLoad():void
		{
			try{
				_widgetManager.unLoadWidget(widgetId);	
			}catch(err:Error){
				trace(err.getStackTrace());
			}
		}
		
		public function reload():void
		{
			try{
				unLoad();
				load();
			}catch(err:Error){
				trace(err.getStackTrace());
			}
		}
		
		public  function set runingParams(value:Object):void{
			this._runingParams = value;
		}
		
		public function get Widget():IWidget
		{
			return _widgetManager.getRegistry().getRegistry().getRegistedWidget(widgetId);
		}
	}
}